package com.cn.wanxi.service.impl;

import com.cn.wanxi.common.RedisNumTool;
import com.cn.wanxi.entity.ResultEntity;
import com.cn.wanxi.entity.UserEntity;
import com.cn.wanxi.mapper.UserMapper;
import com.cn.wanxi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultEntity login(UserEntity userEntity) {
        UserEntity entity = userMapper.login(userEntity);
        if (entity!=null){
            Jedis jedis = new Jedis();
            jedis.select(RedisNumTool.REDIS_LOGIN_SESSION);
            jedis.set("loginUsername",userEntity.getUsername());
        }
        return entity == null ? ResultEntity.failed() : ResultEntity.success(entity);
    }

    @Override
    public ResultEntity add(UserEntity userEntity) {
        int result = userMapper.add(userEntity);
        return ResultEntity.success(result);
    }

    @Override
    public ResultEntity delete(UserEntity userEntity) {
        int result = userMapper.delete(userEntity);
        return ResultEntity.success(result);
    }

    @Override
    public List<UserEntity> findAll(UserEntity userEntity) {
        List<UserEntity> list =  userMapper.findAll(userEntity);
        return list;
    }

    @Override
    public ResultEntity findById(UserEntity userEntity) {
        ResultEntity<UserEntity> entity = ResultEntity.success(userMapper.findById(userEntity));
        return entity;
    }

    @Override
    public Integer getCount(UserEntity userEntity) {
        int count = userMapper.getCount(userEntity);
        return count;
    }

    @Override
    public ResultEntity update(UserEntity userEntity) {
        int result = userMapper.update(userEntity);
        return ResultEntity.success(result);
    }
}
